﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SpielMitMirFinalProject.ModelsGame;
using SpielMitMirFinalProject.ModelViews;
using Microsoft.AspNet.Identity;
using SpielMitMirFinal.SignalR;
using Microsoft.AspNet.SignalR;
using SpielMitMirFinal.Models;

namespace SpielMitMirFinalProject.Controllers
{
    [System.Web.Mvc.Authorize]
    public class GameController : Controller
    {
        private SpielMitMirEntities db = new SpielMitMirEntities();

        // GET: Game/Create
        public ActionResult Game(string myId, int gameId)
        {
            //messages
            IQueryable<ChatM> messagesdb = db.ChatMs.Where(c=>c.GameId==gameId);
            List<ChatMessage> messages = new List<ChatMessage>();
            foreach (ChatM chatM in messagesdb)
            {
                ChatMessage chatMe = new ChatMessage();
                chatMe.sender = chatM.Player.GamerTag;
                chatMe.time = chatM.Time.ToString("hh:mm:ss");
                chatMe.message = chatM.Message;
                chatMe.img = "/images/" + chatM.Player.UserId + ".jpg";
                messages.Add(chatMe);
            }
            //boards
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<GameHub>();
            SquareInformation[,] myMap = new SquareInformation[10,10];
            
            SquareInformation[,] opMap = new SquareInformation[10,10];
           
            int turn = 0;

            Game game = db.Games.Find(gameId);
            Player dbplayer = game.Players.FirstOrDefault(p => p.UserId == myId);
            Player dbOpPlayer = game.Players.FirstOrDefault(p=> p.UserId != myId);
            IEnumerable<Strike> myStrikes = game.Strikes.Where(x => x.PlayerId == dbplayer.PlayerId);
            IEnumerable<Strike> opStrikes = game.Strikes.Where(x => x.PlayerId == dbOpPlayer.PlayerId);
            IEnumerable<ShipPart> myShipParts = db.ShipParts.Where(x => x.Ship.GameId == gameId && x.Ship.PlayerId == dbplayer.PlayerId);
            IEnumerable<ShipPart> opShipParts = db.ShipParts.Where(x => x.Ship.GameId == gameId && x.Ship.PlayerId == dbOpPlayer.PlayerId);
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    myMap[i, j] = new SquareInformation();
                    opMap[i, j] = new SquareInformation();
                    myMap[i,j].x=i;
                    myMap[i,j].y=j;
                    myMap[i,j].type=" empty";
                    opMap[i,j].x=i;
                    opMap[i,j].y=j;
                    opMap[i,j].type=" empty";
                    if (myStrikes.Any(x => x.X == i && x.Y == j))
                    {
                        if (opShipParts.Any(x => x.X == i && x.Y == j))
                            opMap[i, j].type = " hit";
                        else
                            opMap[i, j].type = " miss";
                    }
                    if (myShipParts.Any(x => x.X == i && x.Y == j))
                    {
                        myMap[i, j].type = " ship";
                    }
                    if (opStrikes.Any(x => x.X == i && x.Y == j))
                    {
                        if (myShipParts.Any(x => x.X == i && x.Y == j))
                            myMap[i, j].type = " hit";
                        else
                            myMap[i, j].type = " miss";
                    }
                }
            }
            Strike lastStrike = game.Strikes.LastOrDefault();
            if (lastStrike != null)
            {
                if (lastStrike.PlayerId == dbplayer.PlayerId)
                    turn = 1;
                else
                    turn = 2;
            }
            else
            {
                if (game.State < 0)
                {
                    turn = (game.GameId % 2)+1;
                }
                else
                {
                    if (game.Players.FirstOrDefault() == dbplayer)
                    {
                        turn = 1;
                    }
                    else
                    {
                        turn = 2;
                    }
                }
            }
            ViewBag.Turn = turn;
            ViewBag.MyMap = myMap;
            ViewBag.OpMap = opMap;
            ViewBag.GameId = game.GameId;
            return View(messages);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
